import '../config/kq_core_global.dart';

/// 键值对存储工具
class KqSpUtil {
  /// 保存字符串
  static Future<bool> setString(String key, String? value) {
    if (value == null) {
      return KqCoreGlobal.prefs!.remove(key);
    }
    return KqCoreGlobal.prefs!.setString(key, value);
  }

  /// 获取字符串
  static String? getString(String key, {String? defValue}) {
    return KqCoreGlobal.prefs!.getString(key) ?? defValue;
  }

  /// 保存字符串数组
  static Future<bool> setStringList(String key, List<String>? value) {
    if (value == null) {
      return KqCoreGlobal.prefs!.remove(key);
    }
    return KqCoreGlobal.prefs!.setStringList(key, value);
  }

  /// 获取字符串数组
  static List<String>? getStringList(String key, {List<String>? defValue}) {
    return KqCoreGlobal.prefs!.getStringList(key) ?? defValue;
  }

  /// 保存整数
  static Future<bool> setInt(String key, int? value) {
    if (value == null) {
      return KqCoreGlobal.prefs!.remove(key);
    }
    return KqCoreGlobal.prefs!.setInt(key, value);
  }

  /// 保存整数
  static int? getInt(String key, {int defValue = 0}) {
    return KqCoreGlobal.prefs!.getInt(key) ?? defValue;
  }

  /// 保存浮点数
  static Future<bool> setDouble(String key, double? value) {
    if (value == null) {
      return KqCoreGlobal.prefs!.remove(key);
    }
    return KqCoreGlobal.prefs!.setDouble(key, value);
  }

  /// 获取浮点数
  static double? getDouble(String key, {double defValue = 0}) {
    return KqCoreGlobal.prefs!.getDouble(key) ?? defValue;
  }

  /// 保存布尔
  static Future<bool> setBool(String key, bool? value) {
    if (value == null) {
      return KqCoreGlobal.prefs!.remove(key);
    }
    return KqCoreGlobal.prefs!.setBool(key, value);
  }

  /// 获取布尔
  static bool? getBool(String key, {bool defValue = false}) {
    return KqCoreGlobal.prefs!.getBool(key) ?? defValue;
  }

  /// 删除某个key对应的数据
  static Future<bool> remove(String key) {
    return KqCoreGlobal.prefs!.remove(key);
  }

  /// 判断是否存在某个key对应的数据
  static bool containsKey(String key) {
    return KqCoreGlobal.prefs!.containsKey(key);
  }
}
